package ${controllerUrl};

import com.alibaba.excel.EasyExcel;
import com.erayt.bds.common.BaseConstants;
import com.erayt.bds.listener.EasyExcelListener;
import com.erayt.bds.utils.ExportTools;
import com.erayt.ecas.bean.User;
import ${serviceUrl}.${entityName}Service;
import ${entityUrl}.${entityName};
import ${voUrl}.${entityName}Vo;
import com.alibaba.excel.EasyExcel;
import com.erayt.bds.utils.ExportTools;
import com.erayt.solar.QueryParam;
import com.erayt.solar.SolarMessage;
import com.erayt.solar.core.SolarResult;
import com.erayt.solar.core.parameter.SolarParam;
import com.erayt.solar.db.query.SolarPage;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>说明： ${entityComment}controller层</P>
 * @company erayt
 * @author ${author}
 * @date ${createTime}
*/
@RestController
@RequestMapping("/${objectName}")
public class ${entityName}Controller{
    @Resource
    private ${entityName}Service ${objectName}Service;

    /**
    * description:分页查询
    * @param user
    * @param queryParam
    * @return: com.erayt.solar.db.query.SolarPage<com.erayt.rms.doview.RmsFiveLevelClassifyVo>
    */
    @RequestMapping(value = "/queryPage.action", method= RequestMethod.POST)
    public SolarPage<${entityName}Vo> queryPage(@SolarParam("session-user") User user, @SolarParam QueryParam queryParam) {
        queryParam.put(BaseConstants.LANG, user.getLanguage());
        return ${objectName}Service.queryPageByParam(queryParam);
    }

    /**
     * 根据主键查询信息
     *
     * @param user 用户信息
     * @param primaryKey 主键
     * @return
     */
    @RequestMapping(value = "/selectByPrimaryKey.action", method = RequestMethod.POST)
    public ${entityName}Vo selectByPrimaryKey(@SolarParam("session-user") User user, @SolarParam String primaryKey) {
        return ${objectName}Service.selectByPrimaryKey(user, primaryKey);
    }
    /**
     * 新增${entityComment}
     *
     * @param user         用户信息
     * @param ${objectName} ${entityComment}信息
     * @return
     */
    @RequestMapping(value = "/insert.action", method = RequestMethod.POST)
    public SolarMessage insert(@SolarParam("session-user") User user, @SolarParam ${entityName} ${objectName}) {
        ${objectName}Service.insert(user, ${objectName});
        return SolarResult.None;
    }

    /**
     * 删除${entityComment}
     *
     * @param user 用户信息
     * @param primaryKey 主键
     * @return
     */
    @RequestMapping(value = "/delete.action", method = RequestMethod.POST)
    public SolarMessage delete(@SolarParam("session-user") User user, @SolarParam String primaryKey) {
        ${objectName}Service.delete(user, primaryKey);
        return SolarResult.None;
    }

    /**
     * 修改${entityComment}
     *
     * @param user         用户信息
     * @param ${objectName} ${entityComment}信息
     * @return
     */
    @RequestMapping(value = "/update.action", method = RequestMethod.POST)
    public SolarMessage update(@SolarParam("session-user") User user, @SolarParam ${entityName} ${objectName}) {
        ${objectName}Service.update(user, ${objectName});
        return SolarResult.None;
    }

    /**
     * 导出当前页的全部数据
     * @param response
     * @param pageStart 开始页 =pageNumber=start
     * @param pageLimit 一页限制显示多少条数据 = pageSize = limit
     * @param exportExcelName 导出的excel名
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/exportCurrentPageData.action", method = RequestMethod.GET)
    // todo 注意前端条件参数要加上 并put到queryParam中
    public void exportCurrentPageData(HttpServletResponse response, @SolarParam Integer pageStart, @SolarParam Integer pageLimit,
            @SolarParam String exportExcelName) throws IOException {
        // 分页查询当前页数据
        QueryParam queryParam = ExportTools.getNewQueryParam(pageStart,pageLimit);
        SolarPage<${entityName}Vo> page = ${objectName}Service.queryPageByParam(queryParam);
        List<${entityName}Vo> datals= ExportTools.getSolarPageDatals(page);
        //设置相应数据类型
        response.setContentType("application/octet-stream;charset=UTF-8");
        response.setCharacterEncoding("utf-8");
        //URL编码防止中文乱码
        String fileName = URLEncoder.encode(exportExcelName + "当前页数据", "UTF-8");
        //设置浏览器下载文件而不是渲染解析
        response.setHeader("Content-Disposition","attachment;fileName=" + fileName + ".xlsx");
        ServletOutputStream outputStream = response.getOutputStream();
        //数据写入Excel并响应
        EasyExcel.write(outputStream, ${entityName}Vo.class).sheet("Sheet1").doWrite(datals);
    }


    /**
     * 导出全部数据
     * @param response
     * @param exportExcelName 导出的excel名
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/exportAllData.action", method = RequestMethod.GET)
    public void exportAllData(HttpServletResponse response, @SolarParam String exportExcelName) throws IOException {
        List<${entityName}Vo> list = ${objectName}Service.selectAllData();
        //设置相应数据类型
        response.setContentType("application/octet-stream;charset=UTF-8");
        response.setCharacterEncoding("utf-8");
        //URL编码防止中文乱码
        String fileName = URLEncoder.encode(exportExcelName + "全部数据", "UTF-8");
        //设置浏览器下载文件而不是渲染解析
        response.setHeader("Content-Disposition","attachment;fileName=" + fileName + ".xlsx");
        ServletOutputStream outputStream = response.getOutputStream();
        //数据写入Excel并响应
        EasyExcel.write(outputStream, ${entityName}Vo.class).sheet("Sheet1").doWrite(list);
    }

    /**
     * 导出模板
     * @param response
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/exportTemplate.action", method = RequestMethod.GET)
    public void exportTemplate(HttpServletResponse response, @SolarParam String exportExcelName) throws IOException {
        List<${entityName}> list = ${objectName}Service.createUserExampleData();
        //设置相应数据类型
        response.setContentType("application/octet-stream;charset=UTF-8");
        response.setCharacterEncoding("utf-8");
        //URL编码防止中文乱码
        String fileName = URLEncoder.encode(exportExcelName + "模板", "UTF-8");
        //设置浏览器下载文件而不是渲染解析
        response.setHeader("Content-Disposition","attachment;fileName=" + fileName + ".xlsx");
        ServletOutputStream outputStream = response.getOutputStream();
        //数据写入Excel并响应
        EasyExcel.write(outputStream, ${entityName}.class).sheet("Sheet1").doWrite(list);
    }

    /**
     * 导入数据
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/importData.action", method = RequestMethod.POST)
    public SolarMessage importData(MultipartFile file) throws IOException {
        // 这里 需要指定读用哪个class去读且传递service参数，然后读取第一个sheet 文件流会自动关闭
        EasyExcel.read(file.getInputStream(), ${entityName}.class, new EasyExcelListener<${entityName}>(${objectName}Service)).sheet().doRead();
        return SolarResult.None;
    }

}